﻿using SkiaAndModel.Models;
using SkiaAndModel.NodeAbstract;
using System.Collections.ObjectModel;

namespace SkiaAndModel
{
    public class Tree : VirtualNode
    {
        public int GetHeight()
        {

            return 0;
        }

        /// <summary>
        /// 每个网络的虚拟根节点集合，为了快速定位到网络的根节点
        /// </summary>
        public ObservableCollection<VirtualNode> NetRoot { get; set; } = new ObservableCollection<VirtualNode>();

        public Tree()
        {

            //初始化树，给树的添加第一个网络
            PathNode net1 = new PathNode() { Name = "netPath1" };
            VirtualNode VN1 = new VirtualNode() { Name = "ROOT" };
            net1.From = this;
            net1.To = VN1;
            NetRoot.Add(VN1);
            Children.Add(net1);
            InitMyTree(); //初始化一个点动加自锁的树

            //InitMyTree2(); //这个是我后来调整的一个梯形图，加了一个分支
        }



    public List<string> GetPathArray()
    {   
           List<string> pathArray = new List<string>();
            //网络的遍历
           for(int i = 0 ; i < Children.Count ; i++)
           {
                pathArray.AddRange(GetPathHelper(Children[i].To));
           }
        return pathArray;
    }

    public List<string> GetPathHelper(INode node, List<string> result = null)
        {
            if(result == null)
            {
                result = new List<string>() { "Header"}; //初始化自定义一个头部数据
            }
            //基线条件，如果节点的下一个节点为空并且是一个路径节点，说明找到末尾了
            if(node is PathNode pn0 && pn0.To ==null)
            {
                return result;
            }
            //递归条件，判断节点的类型
            //1 如果节点是虚拟节点类型
            if (node.NodeType == NodeType.VirtualNode && node is VirtualNode vn)
                {
                ArrayHelper(result, vn.Name);
                //第一次进入的时候走这里，因为设定的Root节点为虚拟节点
                if (vn.Children.Count > 1)
                {
                    List<string> temp = new List<string>();
                    for (int j = 0; j < vn.Children.Count; j++)
                    {
                        temp.AddRange(GetPathHelper(vn.Children[j], new List<string>(result)));
                    }
                    return temp;
                }
                else
                { //这里是只有一个子节点的情况
                    return GetPathHelper(vn.Children[0], result);
                }

            }
             //2 如果节点是路径节点类型
              else if (node.NodeType == NodeType.PathNode && node is PathNodeBase pn)
              {
                ArrayHelper(result, pn.Name);
                return GetPathHelper(pn.To, result);
            }
             //3 如果节点是元器件节点类型
              else if(node.NodeType == NodeType.ElementNode && node is ElementNodeBase en)
              {
                ArrayHelper(result, en.Name );
                return GetPathHelper(en.NextPathNode,result);
              }
            else
            {
                throw new Exception("遍历路径异常"); 
            }
              
        }

        public List<string> ArrayHelper(List<string> result,string str)
        {
            for (int i = 0;i<result.Count;i++)
            {
                result[i] +="++"+ str;
            }
            return result;
        }
        //构建一个梯形图
        /// <summary>
        /// 构建一个自锁的简单梯形图
        ///  --|X0|-----（Y0）
        ///  --|Y0|--|
        /// </summary>
        public void InitMyTree()
        {
            //节点信息的声明
            ElementNode M00 = new ElementNode() { Name = "灯开" /*其他信息：类型=常开；地址=M0.0*/};
            ElementNode M01 = new ElementNode() { Name = "灯关" /*其他信息：类型=常闭；地址=M0.1*/ };
            ElementNode M02 = new ElementNode() { Name = "灯(常开)" /*其他信息：类型=常开；地址=M0.2*/ };
            ElementNode M021 = new ElementNode() { Name = "灯(线圈)" /*其他信息：类型=线圈；地址=M0.2*/ };
            VirtualNode vn222 = new VirtualNode() { Name = "虚拟节点" };

            ////母线连接M00与M02
            PathNode line1 = new PathNode();
            PathNode line2 = new PathNode();
            NetRoot[0].Children.Add(line1);
            line1.From = NetRoot[0];
            line1.To = M00;
            NetRoot[0].Children.Add(line2);
            line2.From = NetRoot[0];
            line2.To = M02;

            //// M00与M02连接到虚拟节点
            M00.NextPathNode.To = vn222;
            M02.NextPathNode.To = vn222;

            ///虚拟节连接到M01
            PathNode line3 = new PathNode();
            line3.From = vn222;
            line3.To = M01;
            vn222.Children.Add(line3);

            ///M01（灯关）连接到M021（灯线圈）
            M01.NextPathNode.To = M021;


        }

        public void InitMyTree2()
        {
            
        }

    }
}
